'use strict';

let Koa = require('koa');
let router = require('koa-router')();
let bodyParser = require('koa-bodyparser');

let app = new Koa;
app.use(bodyParser());
app.use(router.routes());

router.get('/', (ctx, next) => {
    ctx.body = `
    <form action="/login" method="post">
        <div>
            <label for="">用户名：</label>
        </div>
        <div>
            <input type="text" name="username">
        </div>
        <div>
            <label for="">密码：</label>
        </div>
        <div>
            <input type="password" name="password">
        </div>
        <div>
            <input type="submit" value="登录">
        </div>
    </form>
    `;
});

router.post('/login', (ctx, next) => {
    let username = ctx.request.body.username;
    let password = ctx.request.body.password;
    console.log(`用户名：${username},密码：${password}`);

    if (username === 'user' && password === '123456') {
        ctx.body = '登陆成功';
    } else {
        ctx.body = '用户名或密码错误';
    };
});

let port = 3000;
app.listen(port);
console.log(`http://localhost:${port}`);